import { getCalcAmount } from '@/utils/common'
import { getSocialRate, calc, getTaxRate, login } from '@/api/index'
import storage from 'store'

const app = {
	state: {
		SystemInfo: {},
		// 计算请求参数
		calcReq: {},
		// 计算结果
		calcResp: {},
		// 五险一金基本信息
		socialRateBase: {},
		// 计算金额
		totalAmount: null,
		// 社保缴纳基数选择
		socialSecuritySelected: 'high',
		socialSecurityAmount: null,
		socialSecurityRateList: null,
		// 公积金基数选择
		housingFundSelected: 'high',
		housingFundAmount: null,
		housingFundRate: null,
		// 专项扣除
		specialDeductionAmount: null,
		specialDeductionSelected: null,
		cityCode: '100100',
		// 年终奖
		yearEndBoundAmount: null,
		// 个税规则
		taxRule: {}
	},
	mutations: {
		SET_SYSTEM_INFO: (state, SystemInfo) => {
			state.SystemInfo = SystemInfo
		},
		SET_SOCIAL_SECURITY_AMOUNT: (state, socialSecurityAmount) => {
			state.socialSecurityAmount = socialSecurityAmount
		},
		SET_TOTAL_AMOUNT: (state, totalAmount) => {
			state.totalAmount = totalAmount
		},
		SET_SOCIAL_SECURITY_AMOUNT: (state, socialSecurityAmount) => {
			state.socialSecurityAmount = socialSecurityAmount
		},
		SET_SOCIAL_SECURITY_RATE_LIST: (state, socialSecurityRateList) => {
			state.socialSecurityRateList = socialSecurityRateList
		},
		SET_SOCIAL_SECURITY_SELECTED: (state, socialSecuritySelected) => {
			state.socialSecuritySelected = socialSecuritySelected
		},
		SET_HOUSING_FUND_AMOUNT: (state, housingFundAmount) => {
			state.housingFundAmount = housingFundAmount
		},
		SET_HOUSING_FUND_RATE: (state, housingFundRate) => {
			state.housingFundRate = housingFundRate
		},
		SET_HOUSING_FUND_SELECTED: (state, housingFundSelected) => {
			state.housingFundSelected = housingFundSelected
		},
		SET_SPECIAL_DEDUCTION_AMOUNT: (state, specialDeductionAmount) => {
			state.specialDeductionAmount = specialDeductionAmount
		},
		SET_SPECIAL_DEDUCTION_SELECTED: (state, specialDeductionSelected) => {
			state.specialDeductionSelected = specialDeductionSelected
		},
		SET_CALC_RESP: (state, calcResp) => {
			state.calcResp = calcResp
		},
		SET_SOCIAL_BASE: (state, socialRateBase) => {
			state.socialRateBase = socialRateBase
		},
		SET_CITY_CODE: (state, cityCode) => {
			state.cityCode = cityCode
		},
		SET_TAX_RULE: (state, taxRule) => {
			state.taxRule = taxRule
		},
		SET_YEAR_END_BOUND_AMOUNT: (state, yearEndBoundAmount) => {
			state.yearEndBoundAmount = yearEndBoundAmount
		}
	},
	actions: {
		// 重置社保公积金数据
		resetSocialSecurityFund({
			commit,
			state
		}) {
			state.socialSecurityAmount = null
			state.housingFundAmount = null
			state.housingFundRate = null
			state.socialSecurityRateList = null
			state.socialSecuritySelected = 'high'
			state.housingFundSelected = 'high'
		},
		calc({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				const {
					totalAmount,
					socialSecurityAmount,
					housingFundAmount,
					housingFundRate,
					socialSecurityRateList,
					specialDeductionAmount,
					socialRateBase,
					yearEndBoundAmount,
					stockAmount
				} = state
				const calcReq = {
					totalAmount: totalAmount,
					socialSecurityAmount: getCalcAmount(state.socialSecuritySelected, totalAmount, socialRateBase.minSocialSecurityAmount, socialRateBase.maxSocialSecurityAmount, socialSecurityAmount),
					housingFundAmount: getCalcAmount(state.housingFundSelected, totalAmount, socialRateBase.minHousingFundAmount, socialRateBase.maxHousingFundAmount, housingFundAmount),
					housingFundRate: housingFundRate ? housingFundRate : socialRateBase.housingFundRate,
					socialSecurityRateList: socialSecurityRateList ? socialSecurityRateList : socialRateBase.socialSecurityRateList,
					specialDeductionAmount: specialDeductionAmount,
					yearEndBoundAmount: yearEndBoundAmount,
					stockAmount: stockAmount
				}
				uni.setStorageSync('storage_calcReq', state);
				calc(calcReq).then(res => {
					commit('SET_CALC_RESP', res.data)
					resolve(res.data)
				})
			}).catch(error => {
				reject(error)
			})
		},
		getSocialRate({
			commit
		}, cityCode) {
			return new Promise((resolve, reject) => {
				getSocialRate(cityCode).then(res => {
					commit('SET_SOCIAL_BASE', res.data)
					resolve(res.data)
				})
			}).catch(error => {
				reject(error)
			})
		},
		/**
		 * 综合计算个税规则
		 */
		getTaxRate({
			commit
		}) {
			return new Promise((resolve, reject) => {
				getTaxRate('1').then(res => {
					commit('SET_TAX_RULE', res.data)
					resolve(res.data)
				})
			}).catch(error => {
				reject(error)
			})
		},
		/**
		 * 年终奖个税规则
		 */
		getYearEndBoundTaxRate({
			commit
		}) {
			return new Promise((resolve, reject) => {
				getTaxRate('2').then(res => {
					resolve(res.data)
				})
			}).catch(error => {
				reject(error)
			})
		}
	}
}

export default app